// pages/mine/other/address.js
var App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: '',
    phone: '',
    username: '',
    isComplete:false,
    addressList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    this.getBoxAddress();
    this.onLoadAddressList();
  },

  //获取宿舍地址
  getBoxAddress: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/users/user/info', //接口地址
      data: {
        token: wx.getStorageSync('token'),
      },
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        if (res.data.error == 0) {
          wx.hideLoading();
          let arr = res.data.data;
          that.setData({
            phone: arr.phone,
            username: arr.nickname,
            address: arr.school + arr.floor + ' ' + arr.hostel,
            isComplete: arr.school==null? false : true
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }, fail(res) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },

  //获取地址列表
  onLoadAddressList: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/users/address/list', 
      data: {
        token: wx.getStorageSync('token'),
      },
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        if (res.data.error == 0) {
          wx.hideLoading();
          that.setData({
            addressList:res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }, fail(res) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //编辑当前地址
  onEditAddress: function (e) {
    wx.navigateTo({
      url: '/pages/mine/other/address_creat?id='+e.currentTarget.dataset.id
    })
  }

})